﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BUS;
using System.Web.Services;
namespace AirPlane
{
    public partial class ThayDoiLichTrinhB2 : System.Web.UI.Page
    {
        protected VeMayBayBUS bus = new VeMayBayBUS();
        protected DataTable dt = new DataTable();
        protected BangGiaVeBUS bus1 = new BangGiaVeBUS();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["islogin"] == null || (bool)Session["islogin"] == false || Session["type"].ToString() != "1")
            {

                Response.Redirect("TrangChu.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    Session["xacnhan"] = 0;
                    if (Request["id"] != null && Request["chuyenbaydi"] != null && Request["chuyenbayve"] != null && Request["hangvedi"] != null && Request["hangveve"] != null && Request["giave"] != null)
                    {
                        dt = bus.tinhPhiChuyenDoiLichTrinh(Request["id"].ToString());

                        float giaVe = 0;
                        float phiThayDoiLichTrinh = 0;
                        if (Request["chuyenbaydi"].ToString() != "" && Request["chuyenbayve"].ToString() == "")
                        {
                            giaVe = bus1.layGiaVe(Request["chuyenbaydi"].ToString(), int.Parse(Request["hangvedi"].ToString()));
                        }
                        else
                            if (Request["chuyenbaydi"].ToString() == "" && Request["chuyenbayve"].ToString() != "")
                            {
                                giaVe = bus1.layGiaVe(Request["chuyenbayve"].ToString(), int.Parse(Request["hangveve"].ToString()));
                            }
                            else
                                if (Request["chuyenbaydi"].ToString() != "" && Request["chuyenbayve"].ToString() != "")
                                {
                                    giaVe = bus1.layGiaVe(Request["chuyenbaydi"].ToString(), int.Parse(Request["hangvedi"].ToString()));
                                    giaVe += bus1.layGiaVe(Request["chuyenbayve"].ToString(), int.Parse(Request["hangveve"].ToString()));
                                }
                                else
                                    if (Request["chuyenbaydi"].ToString() == "" && Request["chuyenbayve"].ToString() == "")
                                    {
                                        lbThongBao.Text = "";//se redirect sau;
                                    }
                        if (dt.Rows.Count == 1)
                        {
                            if (DateTime.Parse(dt.Rows[0]["ngayDi"].ToString()) > DateTime.Now)
                            {
                                phiThayDoiLichTrinh = float.Parse(dt.Rows[0]["phiDoiLichTrinhTruocNgayKhoiHanh"].ToString());
                            }
                            else
                            {
                                phiThayDoiLichTrinh = float.Parse(dt.Rows[0]["phiDoiLichTrinhSauNgayKhoiHanh"].ToString());
                            }
                            txtPhi.Text = phiThayDoiLichTrinh.ToString();
                            txtTraThem.Text = ((giaVe - (float.Parse(Request["giave"]))) * (float.Parse(dt.Rows[0]["phanTramGiaVe"].ToString()))).ToString();
                            txtThue.Text = (float.Parse(txtTraThem.Text) * 0.1).ToString();
                            txtTongCong.Text = (phiThayDoiLichTrinh + float.Parse(txtTraThem.Text) + float.Parse(txtThue.Text)).ToString();
                        }

                    }
                }
            }
        }

        protected void btnQuayLai_Click(object sender, EventArgs e)
        {
            Session["xacnhan"] = 0;
            Response.Redirect("DoiVe.aspx");
        }

        protected void btnXacNhan_Click(object sender, EventArgs e)
        {

            if ((int)Session["xacnhan"] == 0)
            {
                if (Request["id"] != null && Request["chuyenbaydi"] != null && Request["chuyenbayve"] != null && Request["hangvedi"] != null && Request["hangveve"] != null && Request["giave"] != null)
                {
                    string maVeCu = Request["id"].ToString();
                    string chuyenBayCu = "";
                    int hangVeCu = 0;
                    string maKhachHang = "";
                    string maVeDi = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString();
                    string maVeVe = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString();
                    string maChuyenBayDi = Request["chuyenbaydi"].ToString();
                    string maChuyenBayVe = Request["chuyenbayve"].ToString();
                    int hangVeDi = int.Parse(Request["hangvedi"].ToString());
                    int hangVeVe = int.Parse(Request["hangveve"].ToString());
                    float phiDoiLichTrinh = 0;
                    dt = bus.tinhPhiChuyenDoiLichTrinh(maVeCu);
                    string maNhanVien = Session["username"].ToString();
                    if (dt.Rows.Count == 1)
                    {
                        hangVeCu = int.Parse(dt.Rows[0]["ID_loaiVe"].ToString());
                        chuyenBayCu = dt.Rows[0]["ID_chuyenBay"].ToString();
                        maKhachHang = dt.Rows[0]["ID_khach"].ToString();
                        phiDoiLichTrinh = int.Parse(txtPhi.Text);

                        hidKhachHang.Value = maKhachHang;
                    }

                    bus.chuyenDoiLichTrinh(maVeCu, hangVeCu, chuyenBayCu, maKhachHang,maNhanVien, maVeDi, maChuyenBayDi, hangVeDi, maVeVe, maChuyenBayVe, hangVeVe, phiDoiLichTrinh);
                    Session["xacnhan"] = 1;
                    lbThongBao.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Lịch Trình Đã Được Thay Đổi Thành Công!</span>";
                }
            }
            else
            {
                lbThongBao.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Bạn Vừa Xác Nhận Vé Này Rồi!</span>";
            }
        }
        [WebMethod]
        public static string loadVeDaDoiLichTrinh(string maKhachHang)
        {
            string s1 = "";
            DataTable dt2 = new DataTable();
            ChuyenBayBUS cb = new ChuyenBayBUS();
            dt2 = cb.layThongTinVeDoiLichTrinh(maKhachHang, DateTime.Now.ToShortDateString());
            s1 += "<table id ='table-b'>";
            s1 += "<tr >";
            s1 += "<th width= '150px'>Mã Khách Hàng </th>";
            s1 += "<th width= '150px'>Mã Vé</th>";
            s1 += "<th width= '170px'>Họ Tên </th>";
            s1 += "<th width= '130px'>Quốc Tịch </th>";
            s1 += "<th width= '100px'>Ngày Đặt</th>";
            s1 += "<th width= '100px'>Ngày Đi</th>";
            s1 += "<th width= '100px'>Giờ Đi</th>";
          
           

            s1 += "</tr>";

            foreach (DataRow dr in dt2.Rows)
            {
                string maKhachHangDat = dr["ID_khach"].ToString();
                string maVeBay = dr["ID_veBay"].ToString();
                string hoTen = dr["hoTen"].ToString();
                string quocTich = dr["quocTich"].ToString();
                string ngayDat = dr["ngayDat"].ToString();
                string ngayDi = dr["ngayDi"].ToString();
                string gioDi = dr["gioDi"].ToString();
               
             
 
                s1 += "<tr >";

                s1 += "<td width= '150px'>" +maKhachHang+"</td>";
                s1 += "<td width= '150px'>"+maVeBay+"</td>";
                s1 += "<td width= '170px'>"+hoTen+"</td>";
                s1 += "<td width= '130px'>"+quocTich+" </td>";
                s1 += "<td width= '100px'>"+ngayDat+"</td>";
                s1 += "<td width= '100px'>"+ngayDi+"</td>";
                s1 += "<td width= '100px'>"+gioDi+"</td>";
                s1 += "</tr>";
            }
            s1 += "</table>";


            return s1;
        }
    }
}